package com.java.star.mall.front.config;

import com.java.star.mall.front.properties.ThreadPoolExecutorProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 自定义线程池配置
 */
@Configuration
public class ThreadPoolExecutorConfiguration {

    @Bean
    public ThreadPoolExecutor executor(ThreadPoolExecutorProperties threadPoolExecutorProperties) {
        return new ThreadPoolExecutor(
                threadPoolExecutorProperties.getCorePoolSize(),
                threadPoolExecutorProperties.getMaximumPoolSize(),
                threadPoolExecutorProperties.getKeepAliveTime(),
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(threadPoolExecutorProperties.getCapacity()),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }
}
